<?php

    // Include domain-specific classes
    require dirname(__FILE__) . DIRECTORY_SEPARATOR . '_include.php';
    
    try {
        
        //----------------------------------------------------------------------
        // Initialise domain objects
        
        // The Master Datagateway will be the base object to amend functionality.
        $obj_datagateway = new DataGateway_Master();
    
        // The Cache Datagateway will be the extra functionality to combine with
        // the base object.
        
        $obj_datagateway_cache = new DataGateway_Cache();
        
        
        //----------------------------------------------------------------------
        // Create weaved delegator
        
        // Encapsulate the base object within a Weaved Delegator; this will 
        // provide the means to add functionality before a method is invoked.
        
        $obj = new ClassStructure_Weaved_Delegator($obj_datagateway);
        
        // Instruct the Weaved Delegator to invoke the selectPressTitle() method 
        // before the base object has invoked its method; however, break the 
        // chain of methods if the cache's method returns a value.
        
        $obj->__weaveAdviceBefore(
            array($obj_datagateway_cache, 'selectPressTitle'), 
            'selectPressTitle',
            array(
                'break_chain_on_return' => true
            )
        );
        
        //----------------------------------------------------------------------
        // Test functionality
        
        echo "Test: Select press title '145', which is defined in the cache" . "\n";
        echo '---------------------------------------------------------' . "\n\n";
        
        $arr = $obj->selectPressTitle(145);
        
        echo "\n";
        print_r($arr);
        echo "\n";
        
        echo "Test: Select press title '256', which is not defined in the cache" . "\n";
        echo '---------------------------------------------------------' . "\n\n";
        
        $arr = $obj->selectPressTitle(256);
       
        echo "\n";
        print_r($arr);
        echo "\n";
        
        
        echo "Test: Select press title '367', which is not a press title" . "\n";
        echo '---------------------------------------------------------' . "\n\n";
        
        $arr = $obj->selectPressTitle(367);
        
        echo "\n";
        print_r($arr);
        echo "\n";
        
        echo "\n\n";
        
        //----------------------------------------------------------------------
        // Expose structure
        
        echo ClassStructure_UtilityFunctions::getObjectSignature($obj);
        echo "\n";
        echo ClassStructure_UtilityFunctions::getAdviceSignature($obj, 'selectPressTitle');
        
    } catch (Exception $obj_exception) {
        print_r($obj_exception);
    }
    
